import { createRouter, createWebHashHistory } from "vue-router"

import Login from './components/MyLogin.vue'
import Home from './components/MyHome.vue'

import Goods from './components/menus/MyGoods.vue'
import Orders from './components/menus/MyOrders.vue'
import Rights from './components/menus/MyRights.vue'
import Settings from './components/menus/MySettings.vue'
import Users from './components/menus/MyUsers.vue'

import UserDetail from './components/user/MyUserDetail.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login, name: 'login' },
        {
            path: '/home',
            redirect: '/home/users',
            component: Home,
            name: 'home',
            children: [
                { path: 'goods', component: Goods },
                { path: 'orders', component: Orders },
                { path: 'rights', component: Rights },
                { path: 'settings', component: Settings },
                { path: 'users', component: Users },
                { path: 'users/:id', component: UserDetail, props: true }
            ]
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const token = localStorage.getItem('token')
    if (!token) {
        next('/login')
    } else {
        next()
    }
})
export default router